# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=crt
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=12.0.0.r747.g1a99f8514
pkgrel=1
_commit='1a99f85148242b15b5f1fec1d4a318c1869a5816'
pkgdesc='MinGW-w64 CRT for Windows (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.mingw-w64.org/'
msys2_repository_url="https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-crt/"
license=('spdx:ZPL-2.1')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-headers-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip' '!buildflags' '!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit"
        0001-crt-delayimp-Make-an-IAT-entry-writeable-before-modi.patch)
sha256sums=('b1f7f5b22a61a03cafc9d9f6b211451bd9651e3395b1897d2fd52b93b5f1d26c'
            'fcef8b83090c57ff897f471aeb0cd5a71d6385ff569eba8bbef0c424c21acda4')

pkgver() {
  cd "${srcdir}/mingw-w64"
  git describe --long ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${srcdir}/mingw-w64"
  patch -p1 -i "${srcdir}/0001-crt-delayimp-Make-an-IAT-entry-writeable-before-modi.patch"
}

build() {
  cd "${srcdir}/mingw-w64"
  declare -a _extra_config
  case "$CARCH" in
    i686)
      _extra_config=("--disable-lib64" "--enable-lib32")
    ;;
    x86_64)
      _extra_config=("--disable-lib32" "--enable-lib64")
    ;;
    armv7)
      _extra_config=("--disable-lib32" "--disable-lib64" "--enable-libarm32")
    ;;
    aarch64)
      _extra_config=("--disable-lib32" "--disable-lib64" "--disable-libarm32" "--enable-libarm64")
    ;;
  esac

  # only clang+lld support this at the moment
  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    _extra_config+=("--enable-cfguard")
  fi

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] || [[ $MINGW_PACKAGE_PREFIX == *-ucrt-* ]]; then
    _extra_config+=("--with-default-msvcrt=ucrt")
  else
    _extra_config+=("--with-default-msvcrt=msvcrt")
  fi

  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  "${srcdir}"/mingw-w64/mingw-w64-crt/configure \
    --prefix=${MINGW_PREFIX} \
    --disable-dependency-tracking \
    "${_extra_config[@]}"

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install-strip

  # Create empty dummy archives, to avoid failing when the compiler driver
  # adds -lssp -lssh_nonshared when linking.
  ar rcs "${pkgdir}"${MINGW_PREFIX}/lib/libssp.a
  ar rcs "${pkgdir}"${MINGW_PREFIX}/lib/libssp_nonshared.a

  install -Dm644 "${srcdir}"/mingw-w64/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/mingw-w64/COPYING.MinGW-w64/COPYING.MinGW-w64.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64.txt
  install -Dm644 "${srcdir}"/mingw-w64/COPYING.MinGW-w64-runtime/COPYING.MinGW-w64-runtime.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64-runtime.txt
}
